<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>发布删除评论</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        list-style-type: none;
      }
      #box {
        height: 600px;
        width: 600px;
        border: 3px solid #000;
        margin: 100px auto;
      }
      .up {
        height: 100px;
        width: 600px;
        border: 3px solid #000;
        display: flex;
        align-items: center;
        justify-content: center;
        .input {
          height: 50px;
          width: 400px;
          margin-right: 20px;
          font-size: 30px;
          border: 3px solid #000;
        }
        .sent {
          height: 50px;
          width: 150px;
          font-size: 20px;
          border: 3px solid #000;
          cursor: pointer;
        }
      }
      .down {
        height: 495px;
        width: 600px;
        border: 3px solid #000;
        .list {
          padding: 50px;
          li {
            height: 50px;
            width: 500px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 25px;
            border-bottom: 3px solid #000;
            .del {
              color: blue;
              cursor: pointer;
            }
          }
        }
      }
    </style>
  </head>
  <body>
    <div id="box">
      <div class="up">
        <input type="text" class="input" />
        <button class="sent">发布</button>
      </div>
      <div class="down">
        <ul class="list"></ul>
      </div>
    </div>

    <script>
      const inputEle = document.querySelector('.input')
      const sentEle = document.querySelector('.sent')
      const listEle = document.querySelector('.list')

      // 给按钮绑定事件发送输入框的值
      sentEle.addEventListener('click', function () {
        // 当输入框为空时，不发送值
        if (!inputEle.value) {
          return
        }

        // 创建一个li标签，将输入框的值用li包裹
        let newLi = document.createElement('li')
        newLi.innerHTML = inputEle.value + '<span class="del">删除</span>'

        // 将li放入ul中
        listEle.appendChild(newLi)

        // 给li中的删除绑定事件
        let delEles = document.querySelectorAll('.del')
        for (i = 0; i < delEles.length; i++) {
          delEles[i].addEventListener('click', function () {
            this.parentElement.remove()
          })
        }
        inputEle.value = ''
      })
    </script>
  </body>
</html>
